using System.Collections;
using System.Collections.Generic;
using MyProject.Persona;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

public class PhysicsCheck : MonoBehaviour
{

    public bool isGround;
    //public bool touchLeftWall;
    //public bool touchRightWall;
    public bool touchWall;

    public LayerMask groundLayer;
    public LayerMask oneWayPlatform;
    [SerializeField] private LayerMask platformLayer;
    public float radius;

    private CapsuleCollider2D coll;

    [Header("基本参数")]
    //public Vector2 leftOffset;
    //public Vector2 rightOffset;

    public Vector2 bottomOffset;
    public Vector2 faceOffset;

    public Vector2 size;

    [Header("自动")]
    public bool manual;

    private void Awake()
    {
        coll = GetComponent<CapsuleCollider2D>();
    }

    private void Update()
    {
        if (manual)
        {
            //自动设置偏移量
            //Vector2 faceDir = new Vector2(-transform.localScale.x, 0).normalized;
            //bottomOffset = new Vector2((coll.bounds.size.x) / 2 * faceDir.x + (faceDir.x < 0 ? coll.offset.x : -coll.offset.x), 0);
            //leftOffset = new Vector2((coll.bounds.size.x) / 2  + (faceDir.x < 0 ? coll.offset.x : -coll.offset.x) / 2, coll.bounds.size.y / 2);
            //rightOffset = new Vector2(-leftOffset.x, leftOffset.y);
            
            bottomOffset = Vector2.zero;
            // bottomOffset = new Vector2(transform.position.x * faceDir.x , 0);
            faceOffset = new Vector2(coll.bounds.size.x / 2, coll.bounds.size.y / 2);
        }
            Check();

    }

    private void Check()
    {
        // isGround = Physics2D.OverlapCircle((Vector2)transform.position + bottomOffset, radius, groundLayer);
        //touchLeftWall = Physics2D.OverlapCircle((Vector2)transform.position + leftOffset, radius, groundLayer);
        //touchRightWall = Physics2D.OverlapCircle((Vector2)transform.position + rightOffset, radius, groundLayer);
        
        // isGround = Physics2D.OverlapBox((Vector2)transform.position + bottomOffset, size, groundLayer);
        // touchWall = Physics2D.OverlapCircle((Vector2)transform.position + faceOffset * faceDir.x , radius, groundLayer);
        isGround = Physics2D.OverlapCircle((Vector2)transform.position + bottomOffset, radius, groundLayer) 
                   || Physics2D.OverlapCircle((Vector2)transform.position + bottomOffset, radius, oneWayPlatform) 
                   || Physics2D.OverlapCircle((Vector2)transform.position + bottomOffset, radius, platformLayer);
    }

    //OnDrawGizmosSelected是unity特定API，用于编辑界面调试，可视化检测范围
    private void OnDrawGizmosSelected(){
    // {
        // Gizmos.DrawWireSphere((Vector2)transform.position + bottomOffset, radius);
        // Gizmos.DrawWireSphere((Vector2)transform.position + faceOffset * faceDir.x, radius);
        
        Gizmos.DrawWireSphere((Vector2)transform.position + bottomOffset, radius);
        // Gizmos.DrawWireSphere((Vector2)transform.position + faceOffset, radius);
    }
}
